<?php


class AuthoperatorApp extends BackendApp
{
	var $_authoperator_mod;

	function __construct()
	{
		$this->AuthoperatorApp();
	}

	function AuthoperatorApp()
	{
		parent::BackendApp();
		$this->_authoperator_mod =& m('authoperator');
	}

	function index()
	{
		$types=array(
			'0'=>'申请中',
			'1'=>'已认证',
			''=>'全部',
		);
		$this->assign('types', $types);

		$conditions = "1=1 ";
		if($_GET['zao_usertid']!=''){
			$conditions.=" and (m.user_name like '%".$_GET['zao_usertid']."%' || m.real_name like '%".$_GET['zao_usertid']."%')";
		}
		if($_GET['zao_vhcCode']!=''){
			$conditions.=" and m.vhcCode like '%".$_GET['zao_vhcCode']."%'";
		}
		if($_GET['zao_phone_tel']!=''){
			$conditions.=" and m.phone_tel like '%".$_GET['zao_phone_tel']."%'";
		}
		if($_GET['zao_o_name']!=''){
			$conditions.=" and o.o_name like '%".$_GET['zao_o_name']."%'";
		}
		if($_GET['zao_authed']!=''){
			$conditions.=" and a.zao_authed = ".intval($_GET['zao_authed']);
		}
		$unit = $this->visitor->get('unit');
		if($unit!=0){
			$conditions.=" and a.zao_otid=".$unit;
		}

		$db=&db();
		$userrid = $this->visitor->get('rid');
	
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql="SELECT a.*,m.user_name,m.real_name,m.vhcCode,m.phone_tel,o.o_name,o_code,m1.user_name AS zao_auth_opid from zys_authoperator a
LEFT JOIN zys_member m ON a.zao_usertid=m.user_id
LEFT JOIN zys_member m1 ON a.zao_auth_opid=m1.user_id
LEFT JOIN zys_operator o ON a.zao_otid=o.o_tid
		where ".$conditions."  ORDER BY  zao_apply_date DESC LIMIT $page[limit]";
		$authoperator=$this->_authoperator_mod->getAll($sql);

		$sql="select count(1) from zys_authoperator a
LEFT JOIN zys_member m ON a.zao_usertid=m.user_id
LEFT JOIN zys_operator o ON a.zao_otid=o.o_tid
where $conditions";
		$page['item_count']=$this->_authoperator_mod->getOne($sql);   //获取统计数据


		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('authoperator', $authoperator);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

		$this->display('authoperator.index.html');
	}

	function pics(){
		$id=empty($_GET['id'])?0:intval($_GET['id']);
		if($id==0){
			$this->show_warning('用户不存在','',   'index.php?app=authoperator');
			return ;
		}
		$db=&db();
		$sql="SELECT * FROM zys_auth_pic WHERE ap_a_tid=".$id;
		$pics=$db->getAll($sql);
		$this->assign('pics', $pics);
		$this->display('auth.pics.html');
	}

	//删除
	function drop()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if($arrs&&count($arrs)>0){
			foreach ($arrs as $v){
				$temp=$this->_authoperator_mod->get($v);
				if(empty($temp['zao_authed'])){
					$this->_authoperator_mod->drop($v);
				}
			}
		}
		$this->show_message('删除成功','',   'index.php?app=authoperator');
			
	}
	//认证
	function authentication()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);
		if($arrs&&count($arrs)>0){
			foreach ($arrs as $v){
				$this->_authoperator_mod->edit("zao_tid=$v and zao_authed=0 ",array(
				'zao_authed'=>1,
				'zao_auth_opid'=>$this->visitor->get('user_id'),
				'zao_auth_date'=>gmtime()
				));
			}
		}else{
			if (!$this->_authoperator_mod->edit(" zao_tid=$arr ",array('zao_authed'=>1))){
				$this->show_warning($this->_authoperator_mod->get_error(),'',   'index.php?app=authoperator');
				return;
			}
		}
		$this->show_message('认证成功','',   'index.php?app=authoperator');
			
	}

	//认证
	function cancelauthentication()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);
		if($arrs&&count($arrs)>0){
			foreach ($arrs as $v){
				$this->_authoperator_mod->edit("zao_tid=$v ",array(
				'zao_authed'=>0,
				'zao_auth_opid'=>$this->visitor->get('user_id'),
				'zao_auth_date'=>gmtime()
				));
			}
		}else{
			if (!$this->_authoperator_mod->edit(" zao_tid=$arr ",array('zao_authed'=>0))){
				$this->show_warning($this->_authoperator_mod->get_error(),'',   'index.php?app=authoperator');
				return;
			}
		}
		$this->show_message('取消认证成功','',   'index.php?app=authoperator');
			
	}







}
